#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <rclcpp/rclcpp.hpp>
#include <QProcess>
#include <QDebug>
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include <QRadialGradient>
#include <QTimer>
#include <QWebEngineView>
#include <QWebChannel>
#include <QDir>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/opencv.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/compressed_image.hpp>
#include <custom_msgs/msg/gps_data_array.hpp>
#include <sensor_msgs/msg/nav_sat_fix.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
#include <geometry_msgs/msg/twist_stamped.hpp>
#include <ament_index_cpp/get_package_share_directory.hpp>

#include "mapchannel.h"
#include "gaugecolor.h"
#include "gaugecompass.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr, int argc = 0, char** argv = nullptr);
    ~MainWindow();

private:
    // Qt相关变量
    Ui::MainWindow *ui;
    QProcess *roscoreProcess;
    QProcess *startDockProcess;
    QProcess *startDockCamProcess;
    QProcess *startDockCamYoloProcess;
    QProcess *positionControlProcess;
    QTimer *timer_spin;
    QWebChannel *webChannel;
    MapChannel *mapChannel;

    // 初始化相关
    void init_QProcess();
    void init_Variable();
    void init_GUI();
    void init_map();
    void init_UpdateSubscriber();

    // 按钮启动相关
    void startBoatCamOrigin();
    void startBoatCamYolo();
    void startDockCam();
    void startDockCamOrigin();
    void startDockCamYolo();
    void startGoDock();
    void startPositionCtrl();
    
    // 回调函数相关
    void boatCamImageOriginCallback(const sensor_msgs::msg::CompressedImage::SharedPtr msg);
    void boatCamImageYoloCallback(const sensor_msgs::msg::CompressedImage::SharedPtr msg);
    void dockCamImageOriginCallback(const sensor_msgs::msg::Image::SharedPtr msg);
    void dockCamImageYoloCallback(const sensor_msgs::msg::CompressedImage::SharedPtr msg);
    void localPoseCallback(const geometry_msgs::msg::PoseStamped::SharedPtr msg);
    void globalPositionCallback(const sensor_msgs::msg::NavSatFix::SharedPtr msg);
    void localVelocityCallback(const geometry_msgs::msg::TwistStamped::SharedPtr msg);

    // 通过定时器来调用回旋函数
    void rosSpinOnce();

    // 功能函数相关
    void quaternion_to_euler(const geometry_msgs::msg::Quaternion& q, double& roll, double& pitch, double& yaw);

    // ROS相关变量
    rclcpp::Node::SharedPtr node_;
    rclcpp::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr localPoseSubscriber;
    rclcpp::Subscription<sensor_msgs::msg::NavSatFix>::SharedPtr globalPositionSubscriber;
    rclcpp::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr localVelocitySubscriber;
    rclcpp::Subscription<sensor_msgs::msg::CompressedImage>::SharedPtr boatCamOriginImageSubscriber;
    rclcpp::Subscription<sensor_msgs::msg::CompressedImage>::SharedPtr boatCamYoloImageSubscriber;
    rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr dockCamOriginImageSubscriber;
    rclcpp::Subscription<sensor_msgs::msg::CompressedImage>::SharedPtr dockCamYoloImageSubscriber;
    rclcpp::Publisher<custom_msgs::msg::GpsDataArray>::SharedPtr target_pos_pub_;

    // 作为类中的变量传递使用
    double course;
    custom_msgs::msg::GpsDataArray target_gps_array_;

signals:
    void updateBoatPosition(double lng, double lat, double course);
};

#endif // MAINWINDOW_H